package com.jxl.coreplay.net
{
	import com.jxl.coreplay.constants.VideoConstants;
	
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	import flash.net.Responder;

	public class CorePlayNetConnection extends EventDispatcher implements INetConnection
	{
		
		protected var _connectionType:String							= "progressive";
		protected var _connection:INetConnection;
		
		public function get connectionType():String { return _connectionType; }
		public function set connectionType(value:String):void
		{
			if(isValidConnectionType(value) == false) throw new Error("Invalid connection type.  Valid values are progressive and akamai."); return;
			if(_connectionType != value)
			{
				setConnectionType(value);
			}
		}
		
		public function CorePlayNetConnection(value:String="progressive"):void
		{
			super();
			setConnectionType(value);
		}
		
		public function isValidConnectionType(value:String):Boolean
		{
			if(_connectionType != VideoConstants.CONNECTION_PROGRESSIVE && _connectionType != VideoConstants.CONNECTION_AKAMAI)
			{
				return false;
			}
			return true;
		}
		
		protected function setConnectionType(value:String):void
		{
			if(isValidConnectionType(value) == false)
			{
				// TODO: utilize enumeration for printing error
				throw new Error("Invalid connection type.  Valid values are progressive and akamai.");
				return;
			}
			
			var oldValue:String = value;
			_connectionType = value;
		
			switch(value)
			{
				case VideoConstants.CONNECTION_PROGRESSIVE:
					createProgressiveConnection();
					break;
					
				case VideoConstants.CONNECTION_AKAMAI:
					createAkamaiConnection();
					break;
			}
		}
		
		public function destroy():void
		{
			if(_connection != null)
			{
				_connection.destroy();
				_connection = null;
			}
		}
		
		protected function createProgressiveConnection():void
		{
			destroy();
			_connection = new ProgressiveConnection();
		}
		
		protected function createAkamaiConnection():void
		{
			destroy();
			// TODO: create Akamai connection
			//_connection = new ProgressiveConnection();
		}
		
		public function addHeader(operation:String, mustUnderstand:Boolean=false, param:Object=null):void
		{
			_connection.addHeader(operation, mustUnderstand, param);
		}
		
		public function call(command:String, responder:Responder, ...args):void
		{
			var mainArgs:Array = [command, responder];
			var params:Array = mainArgs.concat(args);
			_connection.call.apply(_connection, params);
		}
		
		public function close():void
		{
			_connection.close();
		}
		
		public function connect(url:String, ...args):void
		{
			var urlArray:Array = [url];
			var params:Array = urlArray.concat(args);
			_connection.connect.apply(_connection, params);
		}
		
		public function get client():Object
		{
			return _connection.client;
		}
		
		public function set client(value:Object):void
		{
			_connection.client = value;
		}
		
		public function get connected():Boolean
		{
			return _connection.connected;
		}
		
		public function get connectedProxyType():String
		{
			return _connection.connectedProxyType;
		}
		
		public function get netConnection():NetConnection
		{
			return _connection.netConnection;
		}
		
		public function get objectEncoding():uint
		{
			return _connection.objectEncoding;
		}
		
		public function set objectEncoding(value:uint):void
		{
			_connection.objectEncoding = value;
		}
		
		public function get proxyType():String
		{
			return _connection.proxyType;
		}
		
		public function set proxyType(value:String):void
		{
			_connection.proxyType = value;
		}
		
		public function get uri():String
		{
			return _connection.uri;
		}
		
		public function get usingTLS():Boolean
		{
			return _connection.usingTLS;
		}
		
	}
}